﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblSalutationDAL:ImtblSalutation
    {
        HKEA_CMSEntities model;
        mtblSalutation salutation;

        public mtblSalutationDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static mtblSalutationDAL GetInstance()
        {
            return new mtblSalutationDAL();
        }
        public int InsertENT(mtblSalutation ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblSalutations.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblSalutation> GetListByENT(mtblSalutation ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblSalutations.Where(a => a.SalutationUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.SalutationUID > 0)
                result = result.Where(a => a.SalutationUID == ent.SalutationUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblSalutation> GetListByENT(mtblSalutation ent)
        {
            var result = model.mtblSalutations.Where(a => a.SalutationUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.SalutationUID > 0)
                result = result.Where(a => a.SalutationUID == ent.SalutationUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblSalutation ent)
        {
            var result = model.mtblSalutations.Where(a => a.SalutationUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.SalutationUID > 0)
                result = result.Where(a => a.SalutationUID == ent.SalutationUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblSalutation ent)
        {
            salutation = model.mtblSalutations.Where(a=>a.SalutationUID==ent.SalutationUID).FirstOrDefault();
            if (salutation != null)
            {
                salutation.Deleted = ent.Deleted;
                salutation.LastModifyBy = ent.LastModifyBy;
                salutation.LastModifyDate = DateTime.Now;
                salutation.Salutation_Chi = ent.Salutation_Chi;
                salutation.Salutation_Eng = ent.Salutation_Eng;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblSalutation ent)
        {
            var temp = model.mtblSalutations.Where(a => a.SalutationUID > ent.SalutationUID);
            if (ent.SalutationUID > 0)
                temp = temp.Where(a=>a.SalutationUID==ent.SalutationUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.mtblSalutations.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
